<?php

namespace App\Kuafu\Shop\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Config\Model\ConfigModel;
use App\Kuafu\Shop\Model\ShopUnifiedManagementModel;
use App\Kuafu\Shop\Service\UnifiedManagementService;
use App\Kuafu\User\Service\AuthService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UnifiedManagementController extends BaseController
{
    public function waimaiIndex(Request $request): JsonResponse
    {
        $shopIds  = trim(trim((string)$request->get('shopIds')), ',');
        $pageNum  = (int)$request->get('pageNum', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $shopIds  = strlen($shopIds) ? explode(',', $shopIds) : [];
        return $this->success(UnifiedManagementService::index(ShopUnifiedManagementModel::TYPE_WAIMAI_WHITE, $shopIds, $pageNum, $pageSize));
    }

    public function waimaiImport(Request $request): JsonResponse
    {
        $this->validate($request, [
            'file'    => 'sometimes|file|mimes:xlsx',
            'shopIds' => 'sometimes|array',
        ]);
        $shopIds  = (array)$request->post('shopIds');
        $filepath = (string)optional($request->file('file'))->getRealPath();
        try {
            $list = UnifiedManagementService::waimaiImport($filepath, $shopIds, AuthService::getAuthUser());
        } catch (\Exception $exception) {
            return $this->fail($exception->getCode() ?: 400, $exception->getMessage());
        }
        return $this->success(compact('list'));
    }

    public function waimaiRefresh(Request $request): JsonResponse
    {
        try {
            UnifiedManagementService::waimaiRefresh(AuthService::getAuthUser());
        } catch (\Exception $exception) {
            return $this->fail($exception->getCode() ?: 400, $exception->getMessage());
        }
        return $this->success();
    }

    public function douyinIndex(Request $request): JsonResponse
    {
        $shopIds  = trim(trim((string)$request->get('shopIds')), ',');
        $pageNum  = (int)$request->get('pageNum', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $shopIds  = strlen($shopIds) ? explode(',', $shopIds) : [];
        return $this->success(UnifiedManagementService::index(ShopUnifiedManagementModel::TYPE_DOUYIN_WHITE, $shopIds, $pageNum, $pageSize));
    }

    public function douyinImport(Request $request)
    {
        $this->validate($request, [
            'file'    => 'sometimes|file|mimes:xlsx',
            'shopIds' => 'sometimes|array',
        ]);
        $shopIds  = (array)$request->post('shopIds');
        $filepath = (string)optional($request->file('file'))->getRealPath();
        try {
            $list = UnifiedManagementService::douyinImport($filepath, $shopIds, AuthService::getAuthUser());
        } catch (\Exception $exception) {
            return $this->fail($exception->getCode() ?: 400, $exception->getMessage());
        }
        return $this->success(compact('list'));
    }

    public function douyinHistory(): JsonResponse
    {
        $key   = 'unified_management_history_' . ShopUnifiedManagementModel::TYPE_DOUYIN_WHITE;
        $value = ConfigModel::query()
            ->where('type', 'shop')
            ->where('key', $key)
            ->value('value');
        $list  = (array)json_decode($value, true);
        // 增加一个id字段，方便前端使用
        $i    = 1;
        $list = collect($list)->map(function ($v) use (&$i) {
            $v['id'] = $i++;
            return $v;
        })->sortByDesc(function ($v) {
            return strtotime($v['createdAt']);
        })->values()->toArray();
        return $this->success(compact('list'));
    }

    public function waimaiBlack(): JsonResponse
    {
        $list = UnifiedManagementService::waimaiBlack();
        return $this->success(compact('list'));
    }
}
